﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class Add_HeadSetting : BasePage
    {
        private Qs_HeadBLL qhb = new Qs_HeadBLL();
        private Sys_CompanyBLL scb = new Sys_CompanyBLL();
        private static DataTable dtCompany = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCompany();

                txtFzr.Attributes.Add("onclick", "SelectUser('txtFzr');");
                txtCw.Attributes.Add("onclick", "SelectUser('txtCw');");
            }

            lblCzRen.Text = lognUser.USER_NAME;
            lblCzTime.Text = DateTime.Now.ToString();
        }

        private void setList(DataTable dt, System.Web.UI.WebControls.DropDownList lb)
        {
            int idx = -1;
            dtCompany = dt;
            for (int n = 0; n < dtCompany.Rows.Count; n++)
            {
                if (dtCompany.Rows[n]["CMY_PARENTID"].ToString() == "0")
                {
                    idx = idx + 1;
                    addItem(lb, dtCompany.Rows[n]["CMY_ID"].ToString(), dtCompany.Rows[n]["CMY_NAME"].ToString(), idx);
                    idx = idx - 1;
                }
                else
                {
                    addItem(lb, dtCompany.Rows[n]["CMY_ID"].ToString(), dtCompany.Rows[n]["CMY_NAME"].ToString(), idx);
                }
            }
        }

        private void addItem(System.Web.UI.WebControls.DropDownList lb, String sId, String sName, int idx)
        {
            String sn = sName;
            for (int n = 0; n < idx; n++)
            {
                sn = (char)58853 + "" + sn;
            }
            System.Web.UI.WebControls.ListItem it = new ListItem(sn, sId);

            lb.Items.Add(it);

            for (int n = 0; n < dtCompany.Rows.Count; n++)
            {
                if (dtCompany.Rows[n]["CMY_PARENTID"].ToString() == sId)
                {
                    idx = idx + 1;
                    addItem(lb, dtCompany.Rows[n]["CMY_ID"].ToString(), dtCompany.Rows[n]["CMY_NAME"].ToString(), idx);
                    idx = idx - 1;
                }
            }
        }

        private void BindCompany()
        {
            DataTable dtCompany = scb.GetInfoByPId((int)lognUser.USER_COMPANY);
            if (dtCompany != null)
            {
                setList(dtCompany, txtCompany);
            }
            txtCompany.Items.Insert(0, new ListItem("---请选择---", "-1"));
            txtCompany.SelectedValue = "-1";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Qs_Head model = new Qs_Head();

            if (txtCompany.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择公司");
                return;
            }
            if (string.IsNullOrEmpty(txtFzr.Text.Trim()))
            {
                MessageBox.Show(Page, "请选择公司单位负责人");
                return;
            }
            if (string.IsNullOrEmpty(txtCw.Text.Trim()))
            {
                MessageBox.Show(Page, "请选择公司财务负责人");
                return;
            }

            model.CMY_ID = decimal.Parse(txtCompany.SelectedValue.ToString());
            model.USER_ID = txtFzr.Text.Trim();
            model.FIN_DIRECTOR = txtCw.Text.Trim();
            model.OPER_PEOPLE = lognUser.USER_ID;
            DataTable h = qhb.GetModelById(txtCompany.SelectedValue.ToString());

            if (h != null && h.Rows.Count > 0)
            {
                MessageBox.Show(Page, "该公司已经存在负责人了，您只能修改");
                return;
            }

            if (qhb.InsertModel(model, "") > 0)
            {
                MessageBox.Show(Page, "保存成功");
                return;
            }
            else
            {
                MessageBox.Show(Page, "保存失败");
                return;
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("HeadSetting.aspx");
        }
    }
}